'''
Created on Dec 4, 2010

@author: Nican
'''

from octorotor.ui import scanwin32
import wx

class MenuBar(wx.MenuBar):
    '''
    classdocs
    '''
    
    ValueSaver = None

    def __init__(self):
        '''
        Constructor
        '''
        wx.MenuBar.__init__(self)
        
        self.comSelector = ComSelectorMenu()

        self.Append(self.comSelector, "&COM") # Adding the "filemenu" to the MenuBar


    def getComPort(self):
        port = self.comSelector.getPort()
        
        if self.ValueSaver is not None:
            self.ValueSaver.SetValue("COMPORT", port)
        
        return port
    
    def setValueSaver(self, saver):
        self.ValueSaver = saver
        
        port = saver.GetValue("COMPORT", None)
        
        if port is not None:
            self.comSelector.activatePort( port )
        
        

class ComSelectorMenu(wx.Menu):
    
    
    def __init__(self):
        wx.Menu.__init__(self)
        
        for order, port, desc, hwdid in sorted(scanwin32.comports(False)):
            if order is None:
                continue;
            
            self.AppendRadioItem(order, port, desc)
    
    def activatePort( self, port ):
        for item in self.GetMenuItems():
            if item.GetLabel() == port:
                item.Check(True)
                return
    
    def getPort(self):
        for item in self.GetMenuItems():
            if item.IsChecked():
                return item.GetLabel()
